﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Common.Tests.System.Threading.Tasks
{
    [TestFixture]
    public class NotifierTests
    {
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        [Test]
        public void Capture_progress_and_partial_results()
        {
            int progress = 0;
            List<string> results = new List<string>();
            IEnumerable<string> totalResult = null;

            AsyncPump.Run(async delegate
            {
                IProgressNotifier<IEnumerable<string>> notifier = new Notifier<IEnumerable<string>>((partialResults, prog) =>
                {
                    progress = prog;
                    results.AddRange(partialResults);
                });

                totalResult = OperationWithProgressNotifications(notifier);
            });

            Assert.AreEqual(100, progress);
            CollectionAssert.AreEqual(totalResult, results);
        }

        private IEnumerable<string> OperationWithProgressNotifications(IProgressNotifier<IEnumerable<string>> notifier)
        {
            List<string> results = new List<string>();

            for (int i = 0; i < 10; i++)
            {
                string r = $"Result: {i}";
                notifier.Notify(new[] {r}, (i+1)*10);
                results.Add(r);
            }

            return results;
        }
    }
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
